package com.sun.hunk.struts.action;

import com.sun.hunk.hibernate.DAOFactory;
import com.sun.hunk.hibernate.dao.ItemDAO;
import com.sun.hunk.hibernate.domain.Item;
import com.sun.hunk.hibernate.domain.Category;
import com.sun.hunk.hibernate.domain.User;
import com.sun.hunk.struts.HunkDispatchAction;
import com.sun.hunk.struts.form.ItemForm;
import com.sun.hunk.util.CommonUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author sunil
 */
public class ItemAction extends HunkDispatchAction {

    public ActionForward action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm itemForm = (ItemForm) form;
        if ("Save".equalsIgnoreCase(itemForm.getButton())) {
            return save(mapping, form, request, response);
        } else {
            return search(mapping, form, request, response);
        }
    }

    public ActionForward search(ActionMapping mapping, ActionForm f, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm form = (ItemForm) f;
        ItemDAO dao = new ItemDAO();
        Map<String, Object> map = new HashMap<String, Object>();        
        if (form.getCategoryId() != null && form.getCategoryId()>0) {
            map.put("categarory.id", form.getCategoryId());
        }
        if (CommonUtil.isNotEmpty(form.getCode())) {
            map.put("code", form.getCode());
        }
        if (CommonUtil.isNotEmpty(form.getDescription())) {
            map.put("description", form.getDescription());
        }
        List<Item> list = dao.find(map);
        request.setAttribute("items", list);
        return mapping.findForward("item");
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm itemForm = (ItemForm) form;
        Category catagory = itemForm.getCategory();
        Item item = itemForm.getItem();
        item.setCategarory(catagory);
        String error = validate(itemForm, request);
        if (error != null) {
            setError(request, error);
        } else {
            User user = (User) request.getSession().getAttribute(USER);
            item.setUser(user);
            DAOFactory.getItemDAO().saveOrUpdate(item);
            DAOFactory.getItemDAO().flush();
            setMessage(request, "Item saved successfully.");
        }
        request.setAttribute("itemForm", new ItemForm());        
        search(mapping, new ItemForm(), request, response);
        return mapping.findForward("item");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm itemForm = (ItemForm) form;
        Item item = DAOFactory.getItemDAO().findById(itemForm.getId());
        itemForm.setItem(item);
        search(mapping, new ItemForm(), request, response);
        return mapping.findForward("item");
    }

    public String validate(ItemForm itemForm, HttpServletRequest request) {
        if (CommonUtil.isNull(itemForm.getCategoryId())) {
            return "Select category";
        } else if (CommonUtil.isNull(itemForm.getDescription())) {
            return "Select Item description";
        } else if (CommonUtil.isNull(itemForm.getCode())) {
            return "Select Item code";
        } else if (!isLoggedInUser(request)) {
            return "Please login to add item";
        }
        return null;
    }

    public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm itemForm = (ItemForm) form;
        Item item = DAOFactory.getItemDAO().findById(itemForm.getId());
        request.setAttribute("item", item);
        return mapping.findForward("itemView");
    }
    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {        
        request.removeAttribute("itemForm");
        request.removeAttribute("itemList");
        return mapping.findForward("item");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            ItemForm itemForm = (ItemForm) form;
            DAOFactory.getItemDAO().delete(itemForm.getItem());
            setMessage(request, "Item deleted !");
        } catch (Exception e) {
            setError(request, "oops ! something went wrong.");
        }
        return mapping.findForward("message");
    }

    public ActionForward getTopicsForCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ItemForm itemForm = (ItemForm) form;
        List<Item> items = DAOFactory.getItemDAO().getItemsForCategory(itemForm.getCategoryId());
        request.setAttribute("items", items);
        return mapping.findForward("itemList");
    }
}
